package com.my_library_one.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOperation {
	public static String sdFile = Environment.getExternalStorageDirectory()
			.getPath();

	/**
	 * 把一个Bitmap保存成jpg格式的图片文件
	 * @param context
	 * @param bmp
	 * @param fileName
	 * @return
	 */
	public static String saveBitmapFile(Context context, Bitmap bmp,String fileName) {
		File file = createNewFile(context,fileName);
		FileOutputStream outStream;
		try {
			outStream = new FileOutputStream(file);
			bmp.compress(CompressFormat.JPEG, 100, outStream);
			outStream.close();
			outStream = null;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file.getAbsolutePath();
	}

	public static File saveFile(Context context, byte[] photo,String fileName) {
		FileOutputStream fileOutputStream = null;
		File file = null;
		if (isSDCardExist()) {
			try {
				file = createNewFile(context,fileName);
				fileOutputStream = new FileOutputStream(file);
				fileOutputStream.write(photo);
				fileOutputStream.flush();
			} catch (FileNotFoundException e) {
			} catch (IOException e) {
			} finally {
				try {
					if (fileOutputStream != null) {
						fileOutputStream.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return file;
	}

	/**
	 * 创建一个新文件
	 * @param context
	 * @param fileName
	 *
	 * @return
	 */
	public static File createNewFile(Context context,String fileName){
		String directoryPath = getAppDir(context);
		File file = new File(directoryPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		file = new File(directoryPath, fileName);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file;
	}

	/**
	 * 递归删除文件和文件夹
	 * @param file    要删除的根目录
	 */
	public static void deleteFiles(File file){
		if(file.isFile() && file.exists()){
			file.delete();
			return;
		}
		if(file.isDirectory()){
			File[] childFile = file.listFiles();
			if(childFile == null || childFile.length == 0){
				file.delete();
				return;
			}
			for(File f : childFile){
				deleteFiles(f);
			}
			file.delete();
		}
	}


	public static boolean isSDCardExist() {

		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);

	}
	/**
	 * 获取app的存储目录
	 * <p/>
	 * 一般情况下是这样的/storage/emulated/0/Android/data/包名/
	 *
	 * @param context
	 * @return
	 */
	public static String getAppDir(Context context) {
		return (isSDCardExist() ? (Environment
				.getExternalStorageDirectory().getPath() + "/Android/data/")
				: (context.getCacheDir().getPath()))
				+ context.getPackageName() + "/my_cache/";
	}
	/**
	 * 获取app的存储目录
	 * <p/>
	 * 一般情况下是这样的/storage/emulated/0/Android/data/包名/
	 *
	 * @param context
	 * @return
	 */
	public static String getAppDir(Context context,String foldName) {
		return (isSDCardExist() ? (Environment
				.getExternalStorageDirectory().getPath() + "/Android/data/")
				: (context.getCacheDir().getPath()))
				+ context.getPackageName() + "/"+foldName+"/";
	}
}
